// pages/lawhall/index.js
const {
  LAWHALL
} = require('../../utils/data')
const {
  themessagelist,
  messageprompt,
  business,
  hotdetail,
  adverdetail
} = require('../../utils/api')
const {
  fetch,
  newFetch
} = require('../../utils/fetch')

Page({
  /**
   * 页面的初始数据
   */

  data: {
    value: '',
    activeUrl: 'audit',
    imageURL: '/resources/imgs/my.png',
    activetype: 0,
    messageData: [],
    passData: {},
    notpassData: {},
    specificarea: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (wx.getStorageSync('specificarea')) {
      this.setData({
        specificarea: wx.getStorageSync('specificarea')
      })
    }
    this.selectMessage(1)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  // 1回复     2过期   6  反馈

  selectMessage: function (activetype) {
    let userInfo = wx.getStorageSync('userInfo');
    var params = {
      type: activetype,
      "memberId": userInfo["id"]
    }
    newFetch(`wx/message/${this.data.specificarea}/selectAllMessage`, 'POST', params).then(res => {
      if (res) {
        this.setData({
          messageData: res
        })
      }
    })
  },
  onChange(event) {
    //  搜索 event.detail 为当前输入的值
    this.setData({
      activetype: event.detail.index
    })
    switch (event.detail.index) {
      case 0:
        this.selectMessage(1)
        break
      case 1:
        this.selectMessage(2)
        break
      case 2:
        this.selectMessage(6)
        break
    }
  },

  tabbarClick: function (e) {
    let {
      item
    } = e.currentTarget.dataset
    // wx.navigateTo({
    //   url: '/pages/mymessagedetail/index?item='+JSON.stringify(item)
    // })
    if(item.type == 6) {
      newFetch(`wx/message/${this.data.specificarea}/readMessage`, 'POST', {
        refundId: item.id,
        messageId: item.messageId
      }).then(res => {
        if (res) {
          wx.navigateTo({
            url: '/pages/mymessagedetail/index?id=' + res.messageId,
            success: function (res) {
              // 通过eventChannel向被打开页面传送数据
            }
          })
        }
      })
      return
    }
    newFetch(`wx/message/${this.data.specificarea}/readMessage`, 'POST', {
      refundId: item.id,
      messageId: item.messageId
    }).then(res => {
      if (res) {
        wx.navigateTo({
          url: '/pages/indexDetails/index?id=' + res.messageId,
          success: function (res) {
            // 通过eventChannel向被打开页面传送数据
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
})